/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminOperations;

import CRUD.ForumHome;
import CRUD.MemberHome;
import CRUD.ModeratorpermessionsHome;
import Utility.MessageUtility;

import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Message;

/**
 *
 * @author ABDELMOHSEN
 */
public class AdminManageMembers {
    public void promoteToModerator(Member m,Vector<Forum> vForums,int AdminID){
        MemberHome mHome=new MemberHome();
        ForumHome fHome=new ForumHome();
        ModeratorpermessionsHome pHome=new ModeratorpermessionsHome();
        if(m==null) return;
            if(vForums.size()>0)
            m.setType("mod");
        pHome.persist(m.getModeratorpermessions());
        Set<Member> members=null;
        Forum forum=null;
        String forums=new String();
        for(int i=0;i<vForums.size();i++){
            forum=fHome.findById(vForums.get(i).getForumId());
            members=forum.getMembers();
            members.add(m);
            forum.setMembers(members);
            forums+=forum.getName();
            forums+=", ";
            fHome.persist(forum);
        }
        fHome.session.close();
        pHome.session.close();
        mHome.attachDirty(m);
        String subject="Jets Forums has promoted you to be a Moderator on forums "+forums;
        Member sender=mHome.findById(AdminID);
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        Message msg=new Message(m, sender, "Message From JETS FORUMS", subject, false, sqlDate);
        new MessageUtility().sendMessage(msg);
    }

    public void promotToSuperModerator(int MemID,int AdminID){
        MemberHome mHome=new MemberHome();
        ForumHome fHome=new ForumHome();
        Member m=mHome.findById(MemID);
        if(m==null) return;
        m.setType("smod");
        mHome.persist(m);
        String subject="Jets Forums has promoted you to be a super moderator";
        Member sender=mHome.findById(AdminID);
          java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        Message msg=new Message(m, sender, "Message From JETS FORUMS", subject, false, sqlDate);
        new MessageUtility().sendMessage(msg);
    }

    public void promotToAdmin(int MemID,int AdminID){
        MemberHome mHome=new MemberHome();
        ForumHome fHome=new ForumHome();
        Member m=mHome.findById(MemID);
        if(m==null) return;
        m.setType("adm");
        mHome.persist(m);
        String subject="Jets Forums has promoted you to be an Admin";
        Member sender=mHome.findById(AdminID);
            java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        Message msg=new Message(m, sender, "Message From JETS FORUMS", subject, false, sqlDate);
        new MessageUtility().sendMessage(msg);
        
    }

    public Iterator<Member> searchMembers(String name){
        Iterator<Member> members=null;
        members=new MemberHome().search(name).iterator();
        return members;
    }
    /*public static void main(String[] args){
        ForumHome fh=new ForumHome();
        Vector<Forum> vForums=new Vector<Forum>();
        Forum fff=fh.findById(1);
        vForums.add(fff);
        Forum fff2=fh.findById(2);
        vForums.add(fff2);
        new AdminManageMembers().promoteToModerator(12, vForums, 2);
       // new AdminManageMembers().promotToAdmin(3, 2);
    }*/
}
